/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BD;

import BE.Pedido;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import org.hibernate.criterion.Order;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
/**
 *
 * @author cecv
 */
public class PedidoBD {
     private Session session;
 public PedidoBD() {

         this.session =null;

    }
    @SuppressWarnings("empty-statement")
    public List<Pedido> buscarPedido(int idUsuario,int idCliente) {
//

        List<Pedido> lista = null;
        String consulta="";

        consulta="Select {pedido.*} " +
                "From pedido" +
                " where srl_registradopor = " + idUsuario+
                " and srl_idcliente = " + idCliente+
                " and date_fecharegistro = date_trunc('day',now())"+
                " order by srl_iddocumentotransac asc";

        Transaction tx =null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            SQLQuery sqlquery=session.createSQLQuery(consulta);
            sqlquery.addEntity("pedido",Pedido.class);

            session.flush();
            lista = sqlquery.list();
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            if(tx!=null)
            {
                tx.rollback();
            }
        }
        return lista;

    }
public Integer RegistrarPedido(int codcliente, int codvendedor , int codcondicionpago, String lstproductos, String lstcantidades, String lstprecios, String dia, String mes, String anio) {
        Transaction tx=null;
        try {

            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = (Transaction) session.beginTransaction();
            Connection con= HibernateUtil.getSessionFactory().getCurrentSession().connection();
            CallableStatement cs = con.prepareCall("{call registrarpedido("+codcliente+", "+codvendedor+", "+codcondicionpago+", '{"+lstproductos+"}', '{"+lstcantidades+"}', '{"+lstprecios+"}','"+anio+"-"+mes+"-"+dia+"')}");
           
            ResultSet rs = cs.executeQuery();
            int Resultado=-1;
            while (rs.next())
            {
              Resultado=rs.getInt(1);
            }

            tx.commit();
            return Resultado;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
             if(tx!=null)
            {
                tx.rollback();
            }
            return -4;
        }

    }
}
